/**
 * 
 */
package br.ufrgs.inf.cwsmart.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import br.ufrgs.inf.cwsmart.description.CWSDescription;
import br.ufrgs.inf.cwsmart.exception.FrameworkException;
import br.ufrgs.inf.cwsmart.management.CWSDeploy;
import br.ufrgs.inf.cwsmart.util.Logger;

/**
 * An implementation for the manager.
 * 
 * @author Rodrigo S Lumertz
 *
 */
public class RepositoryManagerImpl implements RepositoryManager {	
	
	/* (non-Javadoc)
	 * @see br.ufrgs.inf.cwsmart.data.RepositoryManager#getCWS(java.lang.String)
	 */
	@Override
	public CWSDeploy getCWS(String idCWS) {
		// TODO: get a specific cws from the db
		System.out.println("TODO: [framework] get a specific cws from the db.");
		
		
		
		return null;
	}

	/* (non-Javadoc)
	 * @see br.ufrgs.inf.cwsmart.data.RepositoryManager#getCWSSet()
	 */
	@Override
	public List<CWSDeploy> getCWSSet() {
		// TODO: get the list of cws
		System.out.println("TODO: [framework] get the set of cws from the database.");
		return null;
	}

	/* (non-Javadoc)
	 * @see br.ufrgs.inf.cwsmart.data.RepositoryManager#persistCWS(br.ufrgs.inf.cwsmart.description.CWSDescription)
	 */
	@Override
	public void persistCWS(CWSDescription cws) throws FrameworkException {		
		
		String strQuery = "INSERT INTO CWS (id_cws, language, cws_path, type) " + 
			"VALUES (E'" + cws.getCWSId() + "',E'" + cws.getLanguage() + "',E'" + cws.getCWSPath() + "'," 
			+ (cws.getCWSType().ordinal() + 1) + ")";
				
		executeNonQuery(strQuery);
	}

	/** 
	 * Executa uma query dada por parametro na base do repositorio
	 * 
	 * @param query
	 */
	@SuppressWarnings("unused")
	private static String executeQuery(String query) 
		throws FrameworkException {
		
		Connection conn = null;
		String result = "";
		
		try {
			conn = RepositoryDBConnection.getInstance().getConnection();	
			PreparedStatement stmt = conn.prepareStatement(query);
								
			ResultSet resSet = stmt.executeQuery();
			if(resSet.next()) {
				result = resSet.getString(1);
			}
			
		} catch (Exception e) {			
			Logger.logger.log(Logger.WARNING_LEVEL, "RepositoryManager.executeQuery", 
					"Exception will be raised because an error was found: " + e.getMessage());
			
			throw new FrameworkException(FrameworkException.ErrorCode.DB_QUERY_EXECUTION_EROR);			
		}
		
		return result;
	}
	
	/** 
	 * Executa uma query dada por parametro na base do repositorio
	 * 
	 * @param query
	 */
	private static void executeNonQuery(String query) 
		throws FrameworkException {
		
		Connection conn = null;
		
		try {
			conn = RepositoryDBConnection.getInstance().getConnection();			
			PreparedStatement stmt = conn.prepareStatement(query);
								
			stmt.execute();
					
			conn.commit();
			conn.setAutoCommit(true);
			
		} catch (Exception e) {			
			Logger.logger.log(Logger.WARNING_LEVEL, "RepositoryManager.executeNonQuery", 
					"Exception will be raised because an error was found: " + e.getMessage());
			
			throw new FrameworkException(FrameworkException.ErrorCode.DB_QUERY_EXECUTION_EROR);										
		}
	}
}

